Page({
  data: {
    groupId: 0,
    group: {},
    imgUrls: [],
    // 轮播图配置参数
    indicatorDots: true, // 是否显示面板指示点
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔，单位为毫秒
    duration: 500, // 滑动动画时长，单位为毫秒
    currentTab: "description",
    countdown: '',
    groupPeopleConfig: ['3人成团', '5人成团', '10人成团', '人成团'],
    isError: false,
    userList: []
  },
   
  switchTab: function (e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
  },
  onLoad: function (options) {
    const id = options.id;
    this.findById(id);
    this.setData({
      groupId: id
    });
  },
  goBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  findById: function (id) {
    console.log("详情页", id);
    wx.request({
      url: `http://localhost:9301/group/findById?id=${id}`,
      method: 'GET',
      success: (res) => {
        const data = res.data.data.group;
        const newImgUrls = [];
        // 检查每个图片链接是否存在并添加到数组中
        if (data.surfacePlot) {
          newImgUrls.push(data.surfacePlot);
        }
        if (data.firstImg) {
          newImgUrls.push(data.firstImg);
        }
        if (data.secondImg) {
          newImgUrls.push(data.secondImg);
        }
        this.setData({
          group: data,
          imgUrls: newImgUrls,
          userList: res.data.data.userList
        });
        console.log('赋值后的 group:', this.data.group);
        console.log('赋值后的 imgUrls:', this.data.imgUrls);
        wx.setNavigationBarTitle({
          title: this.data.group.name
        });
        this.startCountdown(data.endTime);
      },
      fail: (err) => {
        console.error('请求失败:', err);
        this.setData({
          isError: true
        });
      },
      complete: () => {
        console.log('请求完成');
      }
    });
  },
  handleJoinGroup() {
    const id = this.data.groupId;
    wx.navigateTo({
      url: `/pages/joinGroup/joinGroup?id=${id}`,
      success: function () {
        console.log('跳转成功');
      },
      fail: function (err) {
        console.log('跳转失败', err);
      }
    });
  },
  showProductDesc() {

  },
  startCountdown() {
    // 将日期字符串中的 - 替换为 /
    const endTimeStr = this.data.group.endTime.replace(/-/g, '/');
    console.log(endTimeStr);
    const endTime = new Date(endTimeStr).getTime();
    console.log(endTime);
    const intervalId = setInterval(() => {
      const now = new Date().getTime();
      const diff = endTime - now;
      if (diff <= 0) {
        clearInterval(intervalId);
        this.setData({
          countdown: '已结束'
        });
        return;
      }
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((diff % (1000 * 60)) / 1000);
      this.setData({
        countdown: `${days}天 ${hours}小时 ${minutes}分 ${seconds}秒`
      });
    }, 1000);
  }
});